#include "stdafx.h"
#include "AppTools.h"

#include "SettingHelper.h"

TCHAR g_szLogFilePath[MAX_PATH];
HINSTANCE g_HInstance;
CRITICAL_SECTION cs4Log;

void SetAppInstance(HINSTANCE hInstance)
{
	g_HInstance = hInstance;
}

HINSTANCE GetAppInstance()
{
	return g_HInstance;
}

FILE * openLog()
{
	FILE *pLog = _tfopen(g_szLogFilePath,_T("at"));
	if (pLog == NULL)
	{
		return NULL;
	}

	int iRet = fseek(pLog,0,SEEK_END);
	if (iRet != 0)
	{
		fclose(pLog);
		return NULL;
	}
	
	static int iCount = 0;
	if (++iCount < 1000)
	{
		return pLog;
	}
	
	iCount = 0;
	int iLen = ftell(pLog);
	if (iLen <= 0)
	{
		return pLog;
	}
	
	iLen /= 1024;
	if (iLen > 1024 * 5)
	{
		fclose(pLog);
		pLog = _tfopen(g_szLogFilePath,_T("wt"));
	}

	return pLog;
}


void PEWriteLog2File(TCHAR *szLogInfo)
{
	if (szLogInfo == NULL)
	{
		return;
	}

	SYSTEMTIME stStart;
	GetLocalTime(&stStart);
	TCHAR szStartTime[255];
	_stprintf(szStartTime,_T("[%02d-%02d-%04d %02d:%02d:%02d]"),
		stStart.wMonth,stStart.wDay,stStart.wYear,stStart.wHour,stStart.wMinute,stStart.wSecond);

	FILE *pLog = openLog();
	if (pLog == NULL)
	{
		return;
	}
	
	_ftprintf(pLog,_T("%s :%s\r\n"),szStartTime,szLogInfo);

	fflush(pLog);
	fclose(pLog);
}


void PEWriteLog2FileA(char *szLogInfo)
{
	if (szLogInfo == NULL)
	{
		return;
	}

	SYSTEMTIME stStart;
	GetLocalTime(&stStart);
	char szStartTime[255];
	sprintf(szStartTime,"[%02d-%02d-%04d %02d:%02d:%02d]",
		stStart.wMonth,stStart.wDay,stStart.wYear,stStart.wHour,stStart.wMinute,stStart.wSecond);

	FILE *pLog = openLog();
	if (pLog != NULL)
	{
		fprintf(pLog,"%s WMExportBase:%s\n",szStartTime,szLogInfo);
		fclose(pLog);
	}
}


int InitLog()
{
	InitializeCriticalSectionAndSpinCount(&cs4Log,0x80000400);

	memset(g_szLogFilePath,0,MAX_PATH);
	if (0 != GetAppDataPath(g_szLogFilePath))
	{
		_stprintf(g_szLogFilePath,_T(PE_LOG_FILE_NAME));
	}
	else
	{
		_tcscat(g_szLogFilePath,_T(PE_LOG_FILE_NAME));
	}

	return 0;
}

int GetAPPName(TCHAR* szAPPName)
{
	TCHAR szAPPPath[255];
	GetModuleFileName(NULL, szAPPPath, MAX_PATH);
	TCHAR* pData = _tcsstr(szAPPPath, _T("\\"));
	if (pData == NULL)
	{
		return -1;
	}

	_tcscpy_s(szAPPName,255,pData);

	return 0;
}


#include "Shlobj.h"

int GetAppDataPath(TCHAR* szPath)
{
	//	TCHAR tszAppDataPath[_MAX_PATH];  
	SHGetSpecialFolderPath(0,szPath,CSIDL_LOCAL_APPDATA,0);

	_tcscat(szPath,_T("\\mobitnt\\"));
	_tmkdir(szPath);

	_tcscat(szPath,_T("YaSync\\"));
	_tmkdir(szPath);

	return 0;
}

int GetAPPPathEx(TCHAR* szPath)
{
	if (0 == GetModuleFileName(GetAppInstance(), szPath, MAX_PATH))
	{
		return PE_RET_FAIL;
	}

	TCHAR* pData = _tcsrchr(szPath, _T('\\'));
	if (pData == NULL)
	{
		return -1;
	}
	*(++pData) = 0;
	return PE_RET_OK;
}



/*time interval in seconds*/
long GetFileTimeIntervalBySeconds(FILETIME &ft1,FILETIME &ft2)
{
	ULONG64 iFt1 = 0;
	ULONG64 iFt2 = 0;
	ULONG64 iInterval = 0;

	memcpy(&iFt1,&ft1,sizeof(ULONG64));
	memcpy(&iFt2,&ft2,sizeof(ULONG64));

	if (iFt1 > iFt2)
	{
		iInterval = iFt1 - iFt2;
	}
	else
	{
		iInterval = iFt2 - iFt1;
	}

	iInterval = iInterval/ONE_SECOND;/* File time is in 100s of nanoseconds */
	return long(iInterval);
}

void GetNextDay(FILETIME &ft)
{
	SYSTEMTIME st;
	FileTimeToSystemTime(&ft,&st);
	CTime t(st.wYear,st.wMonth,st.wDay,23,59,59);
	t.GetAsSystemTime(st);

	SystemTimeToFileTime(&st,&ft);

	AddFileTimeSpan(ft,(ONE_SECOND + 1)* 60);

}


void GetMidnightTime(FILETIME &ft)
{
	SYSTEMTIME st;
	FileTimeToSystemTime(&ft,&st);

	time_t t = 0;
	memcpy(&t,&ft,sizeof(FILETIME));

	time_t t1 = st.wHour * 60 * 60;
	t1 = t1 * ONE_SECOND;
	t -= t1;

	t1 = st.wMinute * 60;
	t1 = t1 * ONE_SECOND; 
	t -= t1;

	t1 = st.wSecond * ONE_SECOND;
	t -= t1;
	t -= st.wMilliseconds;

	
	memcpy(&ft,&t,sizeof(FILETIME));
}

void GetLocalMidnightTime(FILETIME &ft)
{
	FILETIME ft2;
	FileTimeToLocalFileTime(&ft,&ft2);
	SYSTEMTIME st;
	FileTimeToSystemTime(&ft2,&st);

	time_t t = 0;
	memcpy(&t,&ft2,sizeof(FILETIME));

	time_t t1 = st.wHour * 60 * 60;
	t1 = t1 * ONE_SECOND;
	t -= t1;

	t1 = st.wMinute * 60;
	t1 = t1 * ONE_SECOND; 
	t -= t1;

	t1 = st.wSecond * ONE_SECOND;
	t -= t1;
	t -= st.wMilliseconds;

	
	memcpy(&ft2,&t,sizeof(FILETIME));

	LocalFileTimeToFileTime(&ft2,&ft);
	
}

void AddFileTimeSpan(FILETIME &ft,time_t timeSpan)
{
	time_t iFt1 = 0;
	memcpy(&iFt1,&ft,sizeof(FILETIME));
	iFt1 += timeSpan;
	memcpy(&ft,&iFt1,sizeof(FILETIME));
}

int PECompareFileTime(FILETIME &ft1,FILETIME &ft2)
{
	ULONG64 iFt1 = 0;
	ULONG64 iFt2 = 0;
	ULONG64 iInterval = 0;

	memcpy(&iFt1,&ft1,sizeof(ULONG64));
	memcpy(&iFt2,&ft2,sizeof(ULONG64));

	if (iFt1 > iFt2)
	{
		return PE_RET_CMP_GREATER;
	}

	if (iFt1 < iFt2)
	{
		return PE_RET_CMP_LOWER;
	}

	return PE_RET_CMP_EQUAL;
}


void FmtTimeString(SYSTEMTIME &st,TCHAR *tszTime)
{
	TCHAR szDate[100];
	TCHAR szTime[100];

	GetTimeFormat ( LOCALE_USER_DEFAULT, 0, &st, NULL,szTime, 255);
	GetDateFormat(LOCALE_USER_DEFAULT,0,&st,NULL,szDate,255);

	_stprintf(tszTime,_T("\"%s %s\""),szDate,szTime);
}

void FormatTimeString(__int64 iTimeInSecs,TCHAR *tszTime)
{
	DWORD dwHour = (DWORD)(iTimeInSecs / 3600);
	DWORD dwMin  = (DWORD)((iTimeInSecs - dwHour * 3600)/60);
	int dwSec  = int(iTimeInSecs - dwHour * 3600 - dwMin * 60);

	if (dwHour > 0)
	{
		_stprintf(tszTime,_T(" : %u hours %u mins %u secs"),dwHour,dwMin,dwSec);
		return;
	}

	if (dwMin > 0)
	{
		_stprintf(tszTime,_T(" : %u mins %u secs"),dwMin,dwSec);
		return;
	}

	_stprintf(tszTime,_T(" : %u secs"),dwSec);

	return;
}

void CheckXmlString(TCHAR *str)
{
	if (_tcslen(str) < 1)
	{
		_tcscpy(str,_T("None"));
		return;
	}

	CString sVal = str;

	sVal.Replace(_T("&amp;"),_T("&"));
	sVal.Replace(_T("&lt;"),_T("<"));
	sVal.Replace(_T("&gt;"),_T(">"));
	sVal.Replace(_T("&apos;"),_T("'"));
	sVal.Replace(_T("&quot;"),_T("\""));

	_tcscpy(str,sVal.GetBuffer(-1));
	sVal.ReleaseBuffer();
}


void FreeStringList(list<TCHAR*> strlist)
{
	list<TCHAR*>::iterator it;
	for (it = strlist.begin(); it != strlist.end(); ++it)
	{ 
		TCHAR*p = (TCHAR*)*it;
		free(p);
	}
	strlist.clear();
}

list<TCHAR*> SplitString(TCHAR *srcStr,TCHAR cDiv)
{
	list<TCHAR*> strList;

	int iLen = (int)_tcslen(srcStr);
	TCHAR *str = (TCHAR *)calloc(iLen + 1,sizeof(TCHAR));
	TCHAR *tmp = (TCHAR *)calloc(iLen + 1,sizeof(TCHAR));
	_tcscpy(str,srcStr);

	TCHAR *szStart = str;
	TCHAR *szEnd = NULL;

	int iPos = 0;
	while (iLen-- >= 0)
	{
		if (*szStart == cDiv || iLen < 0)
		{
			tmp[iPos++] = 0;
			if (tmp && _tcslen(tmp) > 0)
			{
				TCHAR *szItem = (TCHAR*)calloc(iPos + 1,sizeof(TCHAR));
				_tcscpy(szItem,tmp);
				strList.push_back(szItem);
			}
			iPos = 0;
		}
		else
		{
			tmp[iPos++] = *szStart;
		}
		++szStart;
	}

	free(str);
	free(tmp);

	return strList;
}

